# SAVE SOMEWHERE SAVE - DELETE lib DIR - RE SYNC


require 'rubygems'
require 'hpricot'
require 'open-uri'

#USAGE: ruby script\runner lib\download_meeting_pdfs.rb <id>
#Example : ruby script\runner lib\download_meeting_pdfs.rb 9
#			This will download all the PDFs for the committee with the id number 9


Dir.chdir(Dir.pwd + File::SEPARATOR + "public" + File::SEPARATOR + "pdfs")
#@meetings = Meeting.find(:all, :conditions =>["committee_id=?", ARGV[0]])
@meetings = Meeting.find(:all)
puts "Checking for text for "+@meetings.size.to_s+ " meetings"
@meetings.each do |meeting|
increment = 1

	committee = Committee.find(:first, :conditions => ["id = ?", meeting.committee_id])
	puts committee.name
	doc = Hpricot(open(meeting.website_link))
	links = doc.search('//a')	
	links.each do |link|
		if (link['href'] =~ /pdf/)
			begin
			open("history.log", "a") { |hist|
				f = "MCC_C" + committee.id.to_s + "_M" + meeting.id.to_s + "_F" + increment.to_s + ".pdf"
				increment = increment + 1
				resp = Net::HTTP.get_response(URI.parse(link['href']))
				open(f, "wb") { |file|
					file.write(resp.body)
					file.close
				}
				hist.write("Downloaded " + link['href'] + " as " + f + "\n")
				puts "Download " + link['href'] + " as " + f + "\n"
				hist.close
			}
			rescue
				open("history.log", "a") { |file|
					file.write("Error with " + link['href'] + ": " + $! + "\n")
					file.close
					puts "Error with " + link['href'] + ")\n\t" + $!
				}	
			end			
		end
	end
end